package hot_100;

public class 缺失的第一个正数 {
    public static void main(String[] args) {

    }

    public int firstMissingPositive(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            while (nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
                int temp = nums[nums[i] - 1];
                nums[nums[i] - 1] = nums[i];
                nums[i] = temp;
            }
        }
        int ans = n + 1;
        for (int i = 0; i < n; i++) {
            if (nums[i] != i + 1) {
                ans = i + 1;
                break;
            }
        }
        return ans;
    }

/*    public int firstMissingPositive(int[] nums) {
        int n = nums.length;
        int vis[] = new int[n + 2];
        for (int i : nums) {
            if (i >= 1 && i < n + 2) {
                vis[i] = 1;
            }
        }
        int ans = 1;
        for (int i = 1; i < vis.length; i++) {
            if (vis[i] == 0) {
                ans = i;
                break;
            }
        }
        return ans;
    }*/
}
